/*
 * @Author: chunlai
 * @Date: 2025-07-17 15:27:29
 * @LastEditTime: 2025-08-07 17:25:52
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \02.bldinsure\src\mixins\popup.js
 * @Description: 底部弹出框
 */
import { mapGetters } from "vuex";
import { key2Mapping, addrKeyMapping } from "@/config/mapping.js";
import { dateToString } from "@/methods/date.js";
export const popupMixin = {
  data() {
    return {
      showPop: false,
      defaultIndex: 0,
      filterCode: "", //过滤省份
      popConfig: {
        list: [],
      },
      keyWord: "", //关键字查询
      minDate: "", //开始日期
      maxDate: "", //结束日期
      currentDate: "",
    };
  },
  computed: {
    ...mapGetters({
      levelName: "dictionary/ocuuClassName",
      provinceFilterArr: "dictionary/provinceFilterArr",
    }),
    viewOptions() {
      return this.popConfig.list.filter(
        (item) => item.text.indexOf(this.keyWord) !== -1
      );
    },
  },
  methods: {
    showPopAction(type, key, list, curVal = "") {
      this.defaultIndex = 0;
      if (this.disabledEdit) return;
      this.filterCode = "";
      if (
        key == "birthDate" &&
        ["0", "3", "20"].includes(this.userInfo.idType) &&
        this.userInfo.idNum
      )
        return;
      if (key == "validDateEnd" && this.validDateDisabled) return;
      if (key == "validDate" && this.validDateStartDisabled) return;
      if (this.provinceFilterArr.includes(key)) {
        this.filterCode = this.filterProvince || "";
      }
      this.popConfig = {
        type,
        key,
        list,
      };
      if (list.length == 1) {
        // 只有一条默认当前值
        this.onConfirmPick(list[0]);
        return;
      }
      if (type == "date") {
        this.currentDate = new Date();
        const dateConfig = this.getDateConfig(key);
        this.minDate = dateConfig.minDate || new Date("2020/10/11");
        this.maxDate = dateConfig.maxDate || new Date("2090/10/1");
        try {
          if (dateConfig.currentDate) {
            this.currentDate = new Date(
              dateConfig.currentDate.replace(/-/g, "/")
            );
          }
        } catch (e) {
          console.error("日期解析失败:", e);
        }
      }
      if (list.length > 0) {
        let defIndex = list.findIndex(
          (item) => curVal == item.keyId || item.default
        );
        if (defIndex > 0) {
          this.defaultIndex = defIndex;
        }
      }
      // console.log(this.currentDate)
      this.$nextTick(() => {
        this.showPop = true;
      });
    },
    // 提取日期配置逻辑
    getDateConfig(key) {
      const defaultConfig = {};
      switch (key) {
        case "birthDate":
          return {
            minDate: this.tbBirthStrMin,
            maxDate: this.tbBirthStrMax,
            currentDate: this.userInfo.birthDate,
          };
        case "validDate":
          return {
            minDate: new Date("1970/10/11"),
            maxDate: new Date(),
            currentDate: this.userInfo.validDate,
          };
        case "validDateEnd":
          return {
            minDate: new Date(),
            maxDate: new Date("2060/10/11"),
            currentDate: this.userInfo.validDateEnd,
          };
        default:
          return defaultConfig;
      }
    },
    onConfirmPick(val) {
      const extraActions = {
        relationToAppnt: () => {
          this.setMaritalStatus();
          this.setSameAddrBtn();
        },
        relationToInsured: () => {
          this.setMaritalStatus();
        },
      };
      const key = this.popConfig.key;
      if (key in key2Mapping) {
        const [field, fieldName] = key2Mapping[key];
        this.changeData({
          [field]: val.keyId,
          [fieldName]: val.text,
        });
        if (extraActions[key]) {
          extraActions[key]();
        }
      }
      this.showPop = false;
    },
    // 日期确认
    onConfirmDate(val) {
      let date = dateToString(val);
      this.changeData({
        [this.popConfig.key]: date,
      });
      this.showPop = false;
      // 证件号年龄查双录
      if (this.userType === "投保人" && this.isNetInsure == "2") {
        this.doubleInputs(`${date}`);
      }
    },
    // 职业确认
    onConfirmDJob(val) {
      const str1 = val.accClass,
        str2 = this.levelName || "类",
        leiming = str1 !== void 0 && str1 !== "" ? `(${str1}${str2})` : "";
      this.changeData({
        occupationCode: val.value,
        occupationCodeName: val.name + `${leiming}`,
        occupationClass: val.accClass,
      });
    },
    // 地址确认
    addresssConfirm(value) {
      // 获取对应的映射关系
      const mapping = addrKeyMapping[this.popConfig.key];
      if (mapping) {
        const data = {};
        for (let [key, prop] of Object.entries(mapping)) {
          data[key] = value[prop] !== undefined ? value[prop] : "";
        }
        this.changeData(data);
        this.showPop = false;
      } else {
        console.warn(`Unknown key: ${this.popConfig.key}`);
      }
    },
  },
};
